<template>
  <div class="image-slider">
    <div class="slides">
      <div
          v-for="(image, index) in images"
          :key="index"
          class="slide"
          :style="{ 'background-image': `url(${image})` }"
          :class="{ 'active': currentIndex === index }"
      ></div>
    </div>
    <button class="prev" @click="prevSlide">Prev</button>
    <button class="next" @click="nextSlide">Next</button>
  </div>
</template>

<script>
export default {
  name: 'ImageSlider',
  data() {
    return {
      images: [
        '@/assets/img_6.png',
        '@/assets/img_7.png',
        '@/assets/img_8.png',
        // 更多图片路径...
      ],
      currentIndex: 0,
      slideInterval: null,
    };
  },
  mounted() {
    this.startAutoPlay();
  },
  beforeDestroy() {
    clearInterval(this.slideInterval);
  },
  methods: {
    startAutoPlay() {
      this.slideInterval = setInterval(this.nextSlide, 3000); // 3秒切换一次图片
    },
    nextSlide() {
      this.currentIndex = (this.currentIndex + 1) % this.images.length;
    },
    prevSlide() {
      this.currentIndex = (this.currentIndex - 1 + this.images.length) % this.images.length;
    },
  },
};
</script>

<style scoped>
.image-slider {
  position: relative;
  width: 100%;
  height: 100px; /* 可以调整高度以适应你的设计 */
  overflow: hidden;
}

.slides {
  display: flex;
  transition: transform 0.5s ease;
}

.slide {
  min-width: 100%;
  height: 100%;
  background-size: cover;
  background-position: center;
  display: none; /* 初始设置为不显示 */
}

.active {
  display: block; /* 仅当添加了 .active 类时显示 */
}

.prev,
.next {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  border: none;
  background-color: rgba(255, 255, 255, 0.5);
  padding: 10px;
  cursor: pointer;
}

.prev {
  left: 10px;
}

.next {
  right: 10px;
}
</style>